<template>
  <div class="com-update-name">
    <van-nav-bar
      title="昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <div class="input-wrap">
      <van-field
        v-model="nickName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      nickName: this.value
    }
  },
  methods: {
    async onClickRight() {
      this.$toast.loading({
        message: '修改中'
      })
      try {
        await updateUserProfile({
          name: this.nickName
        })

        this.$emit('input', this.nickName)
        this.$toast.success('修改成功')
        this.$emit('close')
      } catch (error) {
        this.$toast.fail('昵称重复')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.com-update-name {
  height: 100%;
  background-color: #f5f7f9;
  .input-wrap {
    padding: 10px;
  }
}
</style>
